package com.fqy.servlet;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.hsf.HSFJSONUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Properties;

public class IndexServlet extends  HttpServlet {
    private String Url=null;
    private String UserName=null;
    private String Password=null;
    private String jdbcName=null;
    public Connection con=null;
    public static IndexServlet IS = null;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = IndexServlet.class.getClassLoader().getResourceAsStream("/mysql.properties");
        // 使用properties对象加载输入流
        properties.load(in);
        //获取key对应的value值
        String url1 = properties.getProperty("url");
        String jdbcname1 = properties.getProperty("jdbcname");
        //System.out.println(url1+jdbcname1);
        Url=url1;
        jdbcName=jdbcname1;
        IndexServlet is = new IndexServlet();
        String user = req.getParameter("u");
        String passWord = req.getParameter("p");
        is.UserName = user;
        is.Password = passWord;
        try {
            Class.forName(jdbcName);
            is.con = DriverManager.getConnection(Url, is.UserName, is.Password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        IS=is;
        if(is.con!=null)req.getRequestDispatcher("/main.html").forward(req, resp);
        else req.getRequestDispatcher("/index.html").forward(req, resp);
        //if(con!=null)resp.sendRedirect("http://localhost:8080/untitled_war/main.html");
        //else resp.sendRedirect("http://localhost:8080/untitled_war");
    }
}
